// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/store';
import axios from 'axios';
import Qs from 'qs';

// import { UPLOAD_URL, BASE_URL, OK } from './assets/js/config'
//引入element-ui
//引入ElementUI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI); 

import  VueResource  from 'vue-resource';
Vue.use(VueResource);


import echarts from 'echarts';
Vue.prototype.$echarts = echarts;
// 引入全局组件
import './assets/js/common';
Vue.config.productionTip = false;
Vue.prototype.$bus = new Vue();
// axios使用
Vue.prototype.axios = axios.create({
  baseURL: 'http://ebuc.sosocnc.com/index.php/Home/',
  timeout: 10000,
  withCredentials: true,
  transformRequest: [function (data) {
    data = Qs.stringify(data);
    return data;
  }],
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
 // 上传图片axios请求
Vue.prototype.$axios = axios;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
});

// 全局时间转译函数
Vue.prototype.changeTime = function (value) {
    var timeTo = new Date(value);
    var time = timeTo.getFullYear() + '-' + (timeTo.getMonth() + 1) + '-' + timeTo.getDate() + ' ' + timeTo.getHours() + ':' + timeTo.getMinutes() + ':' + timeTo.getSeconds();
    return time;
};

// 判断是否登录
const Other = ['login', 'registery','forget'];
router.beforeEach((to, from, next) => {
  // 跳转前判断是否登录
  if (Other.join('*').match(to.fullPath.replace('/', '')) || Other.join('*').match(to.name) !== null) {
    next();
  } else {
    Vue.prototype.axios.post('Login/sf_login')
    .then(({data}) => {
      // 如果返回值为2000未登录
      console.log(data);
      if (data.status==='2') {
        sessionStorage.removeItem("isLogin");
        sessionStorage.removeItem("usermes");
        sessionStorage.removeItem("status");
        sessionStorage.removeItem("type");
        router.push({path: 'login'});
      } else if (data.status==='1') {
        sessionStorage.removeItem("isLogin");
        sessionStorage.removeItem("usermes");
        sessionStorage.removeItem("status");
        sessionStorage.removeItem("type");
        router.push({path: 'login'});
      } else {
        next();
      }
    })
    .catch(function (error) {
      console.log(error);
    });
  }
});



